1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 
12 module hip.api.renderer.texture;
13 
14 public import hip.api.data.image;
15 public import hip.api.graphics.color;
16 
17 enum TextureWrapMode : ubyte
18 {
19     CLAMP_TO_EDGE,
20     CLAMP_TO_BORDER,
21     REPEAT,
22     MIRRORED_REPEAT,
23     MIRRORED_CLAMP_TO_EDGE,
24     UNKNOWN
25 }
26 
27 enum TextureFilter : ubyte
28 {
29     LINEAR,
30     NEAREST,
31     NEAREST_MIPMAP_NEAREST,
32     LINEAR_MIPMAP_NEAREST,
33     NEAREST_MIPMAP_LINEAR,
34     LINEAR_MIPMAP_LINEAR
35 }
36 
37 interface IHipTexture
38 {
39     void setWrapMode(TextureWrapMode mode);
40     void setTextureFilter(TextureFilter min, TextureFilter mag);
41 
42     protected bool loadImpl(in IImage img);
43     final bool load(in IImage img)
44     {
45         if(img.hasLoadedData)
46             return loadImpl(img);
47         return false;
48     }
49     void bind(int slot = 0);
50     void unbind(int slot = 0);
51 
52     bool hasSuccessfullyLoaded();
53     void updatePixels(int x, int y, int width ,int height, const(ubyte)[] pixels);
54 
55     int getWidth() const;
56     int getHeight() const;
57 }
58 
59 
60 pragma(LDC_no_typeinfo)
61 struct TextureCoordinatesQuad
62 {
63     float u1, v1, u2, v2;
64 }
65 
66 interface IHipTextureRegion
67 {
68     void setTexture(IHipTexture texture);
69     const(IHipTexture) getTexture() const;
70     IHipTexture getTexture();
71     ///Returns this region width
72     int getWidth() const;
73     ///Returns this region height
74     int getHeight() const;
75 
76     void setRegion(float u1, float v1, float u2, float v2);
77     TextureCoordinatesQuad getRegion() const;
78     ref float[8] getVertices();
79 
80     void setFlippedX(bool flip);
81     void setFlippedY(bool flip);
82     bool isFlippedX();
83     bool isFlippedY();
84 
85     /**
86     *   The uint variant from the setRegion receives arguments in a non normalized way to setup
87     *   the UV coordinates.
88     *   It is better if you wish to just pass where it start and ends.
89     *   The region is divided by the width and height
90     */
91     final void setRegion(int width, int height, uint u1, uint v1, uint u2, uint v2)
92     {
93         float fu1 = u1/cast(float)width;
94         float fu2 = u2/cast(float)width;
95         float fv1 = v1/cast(float)height;
96         float fv2 = v2/cast(float)height;
97         setRegion(fu1, fv1, fu2, fv2);
98     }
99 
100     /**
101     *   The UV coordinates passed are divided by the current texture width and height
102     */
103     final void setRegion(uint u1, uint v1, uint u2, uint v2)
104     {
105         setRegion(getTextureWidth(), getTextureHeight(), u1, v1, u2, v2);
106     }
107 
108 
109 
110     final void setTexture(IHipTexture texture, float u1, float v1, float u2, float v2)
111     {
112         setTexture(texture);
113         setRegion(u1,v1,u2,v2);
114     }
115     
116     final int getTextureWidth() const
117     {
118         const IHipTexture tex = getTexture();
119         if(tex)
120             return tex.getWidth();
121         return 0;
122     }
123     final int getTextureHeight() const
124     {
125         const IHipTexture tex = getTexture();
126         if(tex)
127             return tex.getHeight();
128         return 0;
129     }
130 }